package com.zhouke.chatgpt.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zhouke.chatgpt.Responder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private Responder responder;

    @GetMapping
    public String chat(@RequestParam(defaultValue = "")String message) throws Exception {
        String response=responder.generateResponse(message);
//        String response="{\"id\":\"cmpl-7Eza0oEj6SXJy6XECXkIEX2bHmqTq\",\"object\":\"text_completion\",\"created\":1683807660,\"model\":\"text-davinci-003\",\"choices\":[{\"text\":\"\\n\\n一心一意\",\"index\":0,\"logprobs\":null,\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":28,\"completion_tokens\":8,\"total_tokens\":36}}";
        JSONObject jsonObject=new JSONObject(response);
        JSONArray choices=jsonObject.getJSONArray("choices");
        JSONObject text=choices.getJSONObject(0);
        String answer= StrUtil.trim(text.getStr("text"));
        return answer;
    }
}



















